// GTY


#include "Actor/AuraProjectile.h"

#include "AbilitySystemBlueprintLibrary.h"
#include "AbilitySystemComponent.h"
#include "NiagaraFunctionLibrary.h"
#include "AbilitySystem/AuraAbilitySystemLibrary.h"
#include "auragame/auragame.h"
#include "Components/AudioComponent.h"
#include "Components/SphereComponent.h"
#include "GameFramework/ProjectileMovementComponent.h"
#include "Kismet/GameplayStatics.h"

// Sets default values
AAuraProjectile::AAuraProjectile()
{
 	// 将此 actor 设置为每帧调用 Tick（）。 如果不需要，您可以关闭此功能以提高性能。
	PrimaryActorTick.bCanEverTick = false;

	bReplicates = true;// 设置此 actor 为多客户端同步
	

	Sphere = CreateDefaultSubobject<USphereComponent>("Sphere");// 创建一个球体组件
	SetRootComponent(Sphere);// 将球体组件设置为根组件
	Sphere->SetCollisionObjectType(ECC_Projectile); //设置发射物的碰撞类型(此为自定义类型)
	Sphere->SetCollisionEnabled(ECollisionEnabled::QueryOnly);// 设置碰撞启用为查询
	Sphere->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Ignore);//碰撞通道的响应为忽略
	Sphere->SetCollisionResponseToChannel(ECC_WorldDynamic, ECR_Overlap); //设置其与世界动态物体产生重叠事件
	Sphere->SetCollisionResponseToChannel(ECC_WorldStatic, ECR_Overlap); //设置其与世界静态物体产生重叠事件
	Sphere->SetCollisionResponseToChannel(ECC_Pawn, ECR_Overlap); //设置其与Pawn类型物体产生重叠事件

	//创建发射组件
	ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>("ProjectileMovement");
	ProjectileMovement->InitialSpeed = 550.f; //设置初始速度
	ProjectileMovement->MaxSpeed = 550.f; //设置最大速度
	ProjectileMovement->ProjectileGravityScale = 0.f; //设置重力影响因子，0为不受影响

}

void AAuraProjectile::BeginPlay()
{
	Super::BeginPlay();
	
	SetLifeSpan(LifeSpan);//设置生命周期
	SetReplicateMovement(true);
	Sphere->OnComponentBeginOverlap.AddDynamic(this, &AAuraProjectile::OnSphereOverlap);
	//当 Sphere 组件与其他物体发生碰撞时，会调用 AAuraProjectile::OnSphereOverlap 方法。

	LoopingSoundComponent = UGameplayStatics::SpawnSoundAttached(LoopingSound, GetRootComponent());// 创建一个循环播放的音效组件
	
}

void AAuraProjectile::OnHit()
{
	// 播放声音
	UGameplayStatics::PlaySoundAtLocation(this, ImpactSound, GetActorLocation(),FRotator::ZeroRotator);

	//播放粒子特效
	UNiagaraFunctionLibrary::SpawnSystemAtLocation(this, ImpactEffect, GetActorLocation());

	if (LoopingSoundComponent)
	{
		LoopingSoundComponent->Stop();
		LoopingSoundComponent->DestroyComponent();
	}
	bHit = true;
}

void AAuraProjectile::Destroyed()
{
	if (LoopingSoundComponent)
	{
		LoopingSoundComponent->Stop();
		LoopingSoundComponent->DestroyComponent();
	}

	//如果没有权威性，并且bHit没有修改为true，证明当前没有触发Overlap事件，在销毁前播放击中特效
	if (!bHit && !HasAuthority())
	{
		OnHit();
	}
	Super::Destroyed();
}

void AAuraProjectile::OnSphereOverlap(
	UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex,
	bool bFromSweep, const FHitResult& SweepResult
)
{
	if (!IsValidOverlap(OtherActor)) return;

	if (!bHit)
	{
		OnHit();
	}


	if (HasAuthority())
	{
		//为目标应用GE
		if(UAbilitySystemComponent* TargetASC = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(OtherActor))
		{
			
			//碰撞时获取技能的朝向
			const FVector DeathImpulse = GetActorForwardVector() * DamageEffectParams.DeathImpulseMagnitude;
			DamageEffectParams.DeathImpulse = DeathImpulse;

			//判断是否击退
			if (DamageEffectParams.KnockbackChance >= FMath::RandRange(1, 100))
			{
				// 计算斜向上 30 度的方向
				//const float PitchAngle = 45.f; // 仰角
				//const FRotator LaunchRotation = GetActorRotation() + FRotator(PitchAngle, 0, 0);
				//const FVector KnockbackDirection = LaunchRotation.Vector();

				//const FVector KnockbackForce = KnockbackDirection * DamageEffectParams.KnockbackForceMagnitude;

				
				
				FRotator Rotation = GetActorRotation();//获取当前角色的朝向
				Rotation.Pitch = 15.f;
				const FVector KnockbackDirection = Rotation.Vector();
				//const FVector KnockbackDirection = GetActorForwardVector().RotateAngleAxis(-45.f, GetActorRightVector());
				const FVector KnockbackForce = KnockbackDirection * DamageEffectParams.KnockbackForceMagnitude;
				DamageEffectParams.KnockbackForce = KnockbackForce;
				//UE_LOG(LogTemp, Warning, TEXT("Project KnockbackForce: %s"), *DamageEffectParams.KnockbackForce.ToString());
				//DrawDebugDirectionalArrow(GetWorld(), GetActorLocation(), GetActorLocation() + KnockbackForce * 0.1f, 10.f, FColor::Green, false, 2.f);
			}
			
			DamageEffectParams.TargetAbilitySystemComponent = TargetASC;
			UAuraAbilitySystemLibrary::ApplyDamageEffect(DamageEffectParams);
		}
	
		Destroy();
	}
	else
	{
		bHit = true;
	}
	
}

bool AAuraProjectile::IsValidOverlap( AActor* OtherActor)
{
	if (DamageEffectParams.SourceAbilitySystemComponent == nullptr){ return false; }
	AActor* SourceAvatarActor = DamageEffectParams.SourceAbilitySystemComponent->GetAvatarActor();
	if(SourceAvatarActor == OtherActor)
	{
		return false;
	}
	//如果是友军，返回
	if (!UAuraAbilitySystemLibrary::IsNotFriend(SourceAvatarActor, OtherActor))
	{
		return false;
	}

	return true;
}



